home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / 3D QuickTime Dynamics / kSan Sources / kSanAGWindowDraw.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  29.9 KB  |  1,108 lines

  1. #include "OHScrollableView.h"
  2. #include "kSanAGWindow.h"
  3. #include "kSanSimAtomtypes.h"
  4. void  swapColours( AGWindow *ag)  ;
  5. void  stashColours( AGWindow *ag, RGBColor *rgbs)  ;
  6. void  unstashColours( AGWindow *ag, RGBColor *rgbs)  ;
  7. void  findLinePoints( AGWindow *ag, cubeFrameData *thisFrame,  kSanMatrix * trix, Point offset);
  8. void  setLineOrder(short * lineValPtr, short * lineOrderPtr); // sort by zval
  9. void  checkLineOrder(short index, short * myPtr,short * lineValPtr);
  10. void  drawLines(cubeFrameData *thisFrame, short begin, short end) ;
  11. void  findParticles( AGWindow *ag, particle *basePart, Point offset); //findParticles should not be in header
  12. void  findBonds( AGWindow *ag, particle *basePart, Point offset); //findBonds should not be in header
  13. void  drawContents( AGWindow *ag);
  14. void  sortParticles( AGWindow *ag)  ;
  15.  
  16. void drawHackLines(RGBColor *rgb, cubeFrameData *thisFrame, short begin, short end) ;
  17. void  drawHackContents(RGBColor *rgb, AGWindow *ag);
  18. short  AGWindowDrawHacked(KozoDispatchStack *ds, viewDrawArgs *args);
  19. short  AGWindowDrawRegular(KozoDispatchStack *ds, viewDrawArgs *args);
  20.  
  21. short agCheckOrderList(AGWindow *ag)
  22. {
  23.     if (ag->orderListDirty) 
  24.     {
  25.         AGWindowInitializeWindowOrderList(ag);
  26.      }
  27.     return(noErr);
  28. }
  29. short agCheckPartList(AGWindow *ag)
  30. {
  31.     if (ag->partListDirty) 
  32.     {
  33.         //AGWindowReinitializePartList(ag);
  34.         BitListToOHList( &ag->partBits, &ag->particleList) ;
  35.         ag->partListDirty = false;
  36.      }
  37.     return(noErr);
  38. }
  39. short agCheckGrafDataList(AGWindow *ag)
  40. {
  41.     if (ag->grafDataDirty) 
  42.     {
  43.         AGWindowReinitializeGrafDataList(ag);
  44.      }
  45.     return(noErr);
  46. }
  47.   
  48. short  AGWindowDraw(KozoDispatchStack *ds, viewDrawArgs *args)
  49. {
  50.      AGWindow *ag;
  51.       ag = KDSGetPrivateData(ds, ClassAGWindow );
  52.     if (ag->nowBeingHacked)
  53.     {
  54.         AGWindowDrawHacked(ds,args);
  55.     }
  56.     else
  57.     {
  58.         AGWindowDrawRegular(ds,args);
  59.     }
  60.     return (noErr);
  61. }
  62. CopyGWorldToGWorld (GWorldPtr gwa, GWorldPtr gwb);
  63. CopyGWorldToGWorld (GWorldPtr gwa, GWorldPtr gwb)
  64. {    // must lock pixels outside this routine
  65.     short err = noErr;
  66.  
  67.      PixMapHandle     PixMapA;
  68.      PixMapHandle     PixMapB;
  69.     GWorldFlags AFlags;
  70.     GWorldFlags BFlags;
  71.  
  72.          PixMapA = GetGWorldPixMap(gwa);
  73.         PixMapB = GetGWorldPixMap(gwb);
  74.         AFlags = GetPixelsState(PixMapA);
  75.         BFlags = GetPixelsState(PixMapB);
  76.          LockPixels (PixMapA);
  77.         LockPixels (PixMapB);
  78.  
  79.         CopyBits((BitMapPtr)(*PixMapA),  // from here
  80.               (BitMapPtr)(*PixMapB), &gwb->portRect,&gwb->portRect, adMin   , nilPointer);
  81.  
  82.         SetPixelsState(PixMapA, AFlags);
  83.         SetPixelsState(PixMapB, BFlags);
  84.  //    if (err != 0) doAlert("\pQD Error");
  85.  return (noErr);
  86. }
  87.  
  88.  
  89. short   doAGHackDrawing(AGWindow *ag, particle * basePart, viewDrawArgs *args, RGBColor *rgb);
  90. short   doAGHackDrawing(AGWindow *ag, particle * basePart, viewDrawArgs *args, RGBColor *rgb)
  91. {
  92.         
  93.     cubeFrameData aCubeFrame;
  94.     kSanMatrix trix;
  95.  
  96.      getPrimitiveVectors( ag->sim, &trix);
  97.     findLinePoints( ag, &aCubeFrame, &trix, args->offset);
  98.     setLineOrder(&(aCubeFrame.edgeValues[0]),  &(aCubeFrame.edgeOrder[0]));
  99.     ag->windowView.lastOffsetUsed = args->offset;
  100.  
  101.  
  102.      drawHackLines(rgb, &aCubeFrame, 0 , 7);  // firstEightLines
  103.      PenPat (&qd.black);
  104.     
  105.        findParticles(  ag, basePart, args->offset);
  106.          sortParticles (  ag );
  107.   
  108. //     if (ag->windowView.showBonds == true)  
  109. //    {
  110. //        findBonds(  ag, basePart, args->offset);
  111. //    }
  112.     drawHackContents(rgb,  ag);
  113.  
  114.  
  115. //    drawColourBar(theWindow);   taken out for first compile
  116.     
  117.      drawHackLines(rgb, &aCubeFrame, 8 , 11);  // lastFourLines
  118.     return (noErr);
  119. }
  120.  
  121. short  AGWindowDrawHacked(KozoDispatchStack *ds, viewDrawArgs *args)
  122. {
  123.      grafData *theGrafPtr;
  124.     particle * basePart;
  125.     kozoObject *agObj = ds->obj;
  126.      AGWindow *ag;
  127.      OHBufferedView *bv = bvData(ds->obj);
  128.     kSanMatrix oldTrix;
  129.      RGBColor aColor;
  130.  
  131.     GDHandle     oldGD;
  132.     GWorldPtr    oldGW;
  133.      
  134.  
  135.      ag = KDSGetPrivateData(ds, ClassAGWindow );
  136.      
  137.       theGrafPtr = AGWindowLockGrafData( ag);
  138.     basePart = simLockParts( ag->sim);
  139.     ag->atArray = simLockAtomTypes( ag->sim);
  140.     setRGB (&aColor,65535,30000,30000);
  141.     oldTrix = ag->windowView.viewMatrix;
  142.  
  143.     PenMode (patCopy);
  144.     PenPat (&qd.black);
  145.     
  146.     Spin3dMatrixInDegreesD(&ag->windowView.viewMatrix, 3, 0, 0);
  147.     
  148.  
  149.    
  150.  
  151.     GetGWorld( &oldGW, &oldGD);
  152.     SetGWorld( ag->redChannel, nilPointer);
  153.    
  154.  
  155.     EraseRect(&ag->redChannel->portRect);
  156.     doAGHackDrawing(ag, basePart, args,  &aColor);
  157.     Spin3dMatrixInDegreesD(&ag->windowView.viewMatrix, -6, 0, 0);
  158.     SetGWorld(  ag->blueChannel, nilPointer);
  159.  
  160.  
  161.     EraseRect(&ag->blueChannel->portRect);
  162.     setRGB (&aColor,30000,65535,65535);
  163.     doAGHackDrawing(ag, basePart, args, &aColor);
  164.    
  165.  
  166.       SetGWorld(oldGW,oldGD);
  167.       
  168.        // now copy into the real bv
  169.      CopyGWorldToGWorld (ag->redChannel, bv->world);
  170.      CopyGWorldToGWorld (ag->blueChannel, bv->world);
  171.     ag->windowView.viewMatrix = oldTrix;
  172.       
  173.     PenPat (&qd.black);
  174.  
  175.     AGWindowReleaseGrafData( ag);
  176.     simReleaseParts( ag->sim);
  177.     simReleaseAtomTypes( ag->sim);
  178.      KDSReturnContinue(ds);
  179. }
  180. short  AGWindowDrawRegular(KozoDispatchStack *ds, viewDrawArgs *args)
  181. {
  182.      grafData *theGrafPtr;
  183.     particle * basePart;
  184.     kozoObject *agObj = ds->obj;
  185.      AGWindow *ag;
  186.     kSanMatrix trix;
  187.     cubeFrameData aCubeFrame;
  188.         
  189.      ag = KDSGetPrivateData(ds, ClassAGWindow );
  190.      
  191.       theGrafPtr = AGWindowLockGrafData( ag);
  192.     basePart = simLockParts( ag->sim);
  193.     ag->atArray = simLockAtomTypes( ag->sim);
  194.  
  195.  
  196.     PenMode (patCopy);
  197.     PenPat (&qd.black);
  198.      getPrimitiveVectors( ag->sim, &trix);
  199.     findLinePoints( ag, &aCubeFrame, &trix, args->offset);
  200.     setLineOrder(&(aCubeFrame.edgeValues[0]),  &(aCubeFrame.edgeOrder[0]));
  201.     ag->windowView.lastOffsetUsed = args->offset;
  202.  
  203.  
  204. #ifndef __2DVectors__   
  205.     drawLines(&aCubeFrame, 0 , 7);  // firstEightLines
  206. #else
  207.     drawLines(&aCubeFrame, 0 , 3);  // all four Lines
  208. #endif
  209.     PenPat (&qd.black);
  210.     
  211.  
  212.     if (ag->doColourCalc)
  213.     {
  214.         findParticles(  ag, basePart, args->offset);
  215.         
  216. #ifndef __2DVectors__    
  217.         sortParticles( ag);
  218. #endif
  219.  
  220.          if (ag->cpi) ag->cpi->colourParticleList (  ag->cpi,  ag->sim, theGrafPtr, AGWindowNumParts(ag));
  221.      }
  222.     else
  223.     {
  224.           RGBColor *tempRGB = nilPointer;
  225.         long count = AGWindowNumParts(ag);
  226.         tempRGB = (RGBColor *) NewPtr (sizeof(RGBColor) * count);
  227.         if (tempRGB != nilPointer) stashColours( ag, tempRGB);
  228.         findParticles(  ag, basePart, args->offset);
  229.          if (tempRGB != nilPointer) unstashColours(  ag, tempRGB);
  230.  #ifndef __2DVectors__    
  231.         sortParticles (  ag );
  232. #endif
  233.         if (tempRGB != nilPointer) DisposePtr ((Ptr)tempRGB);
  234.      }
  235.  
  236.  
  237.      if (ag->windowView.showBonds == true)  
  238.     {
  239.         findBonds(  ag, basePart, args->offset);
  240.     }
  241.     drawContents( ag);
  242.  
  243.  
  244. //    drawColourBar(theWindow);   taken out for first compile
  245.     
  246.   
  247.  
  248. #ifndef __2DVectors__  
  249.     drawLines(&aCubeFrame, 8 , 11);  // lastFourLines
  250. #endif
  251.  
  252.     PenPat (&qd.black);
  253.  
  254.     AGWindowReleaseGrafData( ag);
  255.     simReleaseParts( ag->sim);
  256.     simReleaseAtomTypes( ag->sim);
  257.  
  258.      KDSReturnContinue(ds);
  259. }
  260.  
  261.  
  262. void      stashColours(AGWindow *ag, RGBColor *rgbs)  
  263. {
  264.     long count;
  265.     long i;
  266. //     long *orderListPtr;
  267.     grafData * theGrafPtr;
  268.  
  269.      agCheckOrderList(ag);
  270.     theGrafPtr = AGWindowLockGrafDataC( ag, &count);
  271. //     orderListPtr = lockList( &ag->orderList);
  272.     for (i = 0; i< count; ++i)
  273.     {
  274.         rgbs[i] = theGrafPtr[i].colour;
  275.     }
  276.     AGWindowReleaseGrafData( ag);
  277. //     releaseList( &ag->orderList);    
  278.  
  279. }
  280.  
  281. void      unstashColours(AGWindow *ag, RGBColor *rgbs)  
  282. {
  283.     long count;
  284.     long i;
  285.     long *orderListPtr;
  286.     grafData * theGrafPtr;
  287.     count = AGWindowNumParts(ag);
  288.  
  289.  
  290.     agCheckOrderList(ag);
  291.     theGrafPtr = AGWindowLockGrafData( ag);
  292.     orderListPtr = lockList( &ag->orderList);
  293.     for (i = 0; i< count; ++i)
  294.     {
  295.           theGrafPtr[i].colour = rgbs[orderListPtr[i]];
  296.     }
  297.     AGWindowReleaseGrafData(  ag);
  298.     releaseList( &ag->orderList);    
  299.  
  300. }
  301.  
  302. #ifndef __2DVectors__ 
  303. double      agParticleGetSortingValue(anyList *list, long index, void *param) ; 
  304. double      agParticleGetSortingValue(anyList *list, long index, void *param)  
  305. {
  306.     grafData *theGrafPtr = (grafData *) param;
  307.     long theListValue = ((long *)(*list->listData))[index];
  308.     return(theGrafPtr[theListValue].graphc );
  309. }
  310.  
  311. void      sortParticles(AGWindow *ag)  
  312. {
  313.     grafData *theGrafPtr;
  314.  
  315.     agCheckOrderList(ag);
  316.      theGrafPtr = AGWindowLockGrafData( ag );
  317.     if (theGrafPtr)  anylistValueSort((anyList *)&ag->orderList, agParticleGetSortingValue, (short (*) (anyList *, long, long)) OHListSwapItemsFunc, theGrafPtr);
  318.      AGWindowReleaseGrafData( ag ); 
  319. }
  320. #endif
  321.  
  322. /*
  323. void      sortParticles(AGWindow *ag)  
  324. {
  325.     long * orderList;
  326.     lksPtr firstlink;
  327.     lksPtr thislink;
  328.     lksPtr somelink;
  329.     lksPtr nextlink;
  330.     lksPtr myPtr;
  331.     long i = 0;
  332.     long j = 0;
  333.     long count;
  334.  
  335.     count = OHListCount(&ag->partList);
  336.     if (count > 0)
  337.     {
  338.         myPtr = simLockLinkArray(  ag->sim);
  339.         orderList = lockList( &ag->orderList);
  340.         firstlink = &(myPtr[0]);
  341.         firstlink->label = 0;
  342.         firstlink->value = theGrafPtr[i].graphc;
  343.         firstlink->next = nilPointer;
  344.         
  345.         for (i=1 ; i < count ; ++i)
  346.         {
  347.             thislink = &(myPtr[i]);
  348.             thislink->label = i;
  349.             thislink->value = theGrafPtr[i].graphc;
  350.             if (thislink->value >= firstlink->value)  // firstlink has highest value of stack
  351.             {
  352.                 thislink->next = firstlink;  // make thislink the top of stack
  353.                 firstlink = thislink;
  354.             }
  355.             else 
  356.             {
  357.                    somelink = firstlink;  // find place in stack
  358.                    nextlink = somelink->next;
  359.                    while ((nextlink != nilPointer) &&  (thislink->value < nextlink->value) )
  360.                                
  361.                    {
  362.                      somelink = nextlink;
  363.                            nextlink = somelink->next;
  364.                    }
  365.                    
  366.         // we drop through this while statement when we are at the end of the list or when  
  367.         // the value of thislink is in between somelink and nextlink                  
  368.  
  369.                   thislink->next = nextlink;
  370.                   somelink->next = thislink; 
  371.             }
  372.         }
  373.         somelink = firstlink;
  374.         for(i= (OHListCount(&ag->partList)) -1 ; i>=0 ; i = i-1)
  375.         {    
  376.             orderList[i] = somelink->label;
  377.             somelink = somelink->next;        
  378.         }
  379.         AGWindowReleaseGrafData(  ag );
  380.         simReleaseLinkArray(  ag->sim);
  381.         releaseList( &ag->orderList );
  382.         ag->orderListDirty = false;
  383.     }
  384. }
  385. */
  386. void findLinePoints(AGWindow *ag, cubeFrameData *thisFrame,  kSanMatrix * trix, Point offset)
  387. {
  388.  
  389.      short j,k;
  390.      kSanVector lineBeginning;
  391.     kSanVector lineEnd;
  392.     kSanVector linePointOne;
  393.     kSanVector linePointTwo;
  394.      // depthFactor, viewLen;
  395.     //float mag;
  396.      viewInfo *vInfo;
  397.     OHView *view = viewData (ag->win->obj);
  398.     
  399.     kSanMatrix cMat;
  400.     kSanMatrix * theMatPtr;
  401.     vInfo = &ag->windowView;
  402.      theMatPtr = &(vInfo->viewMatrix);
  403.  
  404.     //mag = vInfo->magnification;
  405.     
  406.     getCramerskSanOperatorsD(theMatPtr, &cMat);
  407.     multkSanMatrixD( &cMat ,  vInfo->magnification,  &cMat);
  408.  
  409.     for (j= 0; j< NUMCUBEEDGES ;++j)
  410.     {
  411.         kSanVector startCorner;
  412.         kSanVector endCorner;
  413.        
  414.              
  415.         thisFrame->edgeValues[j] = 0;
  416.         startCorner = gCubeVerticies[gCubeEdges[j].start];
  417.         endCorner = gCubeVerticies[gCubeEdges[j].end];
  418.  
  419.          vecDotkSanMatrixD( &startCorner, trix, &lineBeginning);
  420.          vecDotkSanMatrixD( &endCorner, trix, &lineEnd);
  421.  
  422.          for (k= 0; k<=5 ;++k)
  423.         {
  424.             float multFactorOne = ((float)k) / 5.0 ;
  425.             float multFactorTwo = ((float)(5 - k)) / 5.0 ;
  426.             kSanVector vecSum;
  427.             kSanVector graphicsCoords;
  428.             multkSanVectorD(&lineBeginning, multFactorOne, &linePointOne);
  429.             multkSanVectorD(&lineEnd, multFactorTwo, &linePointTwo);
  430.             addkSanVectorsD (&linePointOne , &linePointTwo, &vecSum);
  431.  
  432.             cramersCoordsOfkSanPoint ( &cMat, &vecSum, &graphicsCoords);
  433.   
  434.                                  
  435. #ifndef __2DVectors__
  436.             thisFrame->edgeValues[j] += graphicsCoords.c;
  437. #endif
  438. //            if (graphC  < viewLen)   // floatToShort handles nans
  439.             {
  440.                 float aFloat, bFloat;
  441.                  bFloat = (-  graphicsCoords.b)  +  offset.v;
  442.                 aFloat = (   graphicsCoords.a) +  offset.h;
  443.                 thisFrame->edge[j].edgePoints[k].v = floatToShort (bFloat);
  444.                 thisFrame->edge[j].edgePoints[k].h = floatToShort (aFloat);
  445.             }
  446.         }
  447.     }
  448.  }
  449.  
  450. void setLineOrder(short * lineValPtr, short * lineOrderPtr)  // sort by zval
  451. {
  452.     short i;
  453.     for (i = 0;i<NUMCUBEEDGES;++i)
  454.     {
  455.         lineOrderPtr[i] = i;
  456. #ifndef __2DVectors__
  457.         checkLineOrder(i, lineOrderPtr, lineValPtr);
  458. #endif
  459.     }
  460. }
  461. void  checkLineOrder(short index, short * myPtr,short * lineValPtr)
  462. {
  463.      short temp;
  464.     if (index > 0)
  465.     {
  466.         if (lineValPtr[myPtr[index]] < lineValPtr[myPtr[index-1]])
  467.         {
  468.             temp = myPtr[index];
  469.             myPtr[index] = myPtr[index-1];
  470.             myPtr[index-1] = temp;
  471.             checkLineOrder((index-1), myPtr, lineValPtr);
  472.         }
  473.     }
  474.  }
  475. void drawLines(cubeFrameData *thisFrame, short begin, short end) 
  476. {
  477.      int i, j;
  478.     short whichEdge;
  479.     LinePair *myPair;
  480.     RGBColor lpColours[4] = {{0,0,0}, {0xFFFF, 0x3FFF, 0x3FFF},  {0x3FFF, 0xFFFF, 0x3FFF},  {0x3FFF, 0x3FFF, 0xFFFF}};
  481.     Point *myPoint;
  482.     PenSize(2,2);
  483.     PenPat(&(qd.dkGray));
  484.     for (i = begin; i<=end; ++i)
  485.     {
  486.         whichEdge = thisFrame->edgeOrder[i];
  487.         myPair = &(gCubeEdges[whichEdge]);  // retrieve the pair from the global list
  488.         RGBForeColor(&lpColours[myPair->colour]); 
  489.         myPoint = &(thisFrame->edge[whichEdge].edgePoints[0]);
  490.         MoveTo(myPoint->h, myPoint->v);
  491.         for (j = 1; j <= 5 ; ++j)
  492.         {
  493.            myPoint = &(thisFrame->edge[whichEdge].edgePoints[j]);
  494.            LineTo(myPoint->h, myPoint->v);        
  495.         }
  496.     }
  497.     PenSize(1,1);
  498.  }
  499. void drawHackLines(RGBColor *rgb, cubeFrameData *thisFrame, short begin, short end) 
  500. {
  501.      int i, j;
  502.     short whichEdge;
  503.     LinePair *myPair;
  504.      Point *myPoint;
  505.     PenSize(2,2);
  506.     PenPat(&(qd.dkGray));
  507.     RGBForeColor(rgb); 
  508.     for (i = begin; i<=end; ++i)
  509.     {
  510.         whichEdge = thisFrame->edgeOrder[i];
  511.         myPair = &(gCubeEdges[whichEdge]);  // retrieve the pair from the global list
  512.         myPoint = &(thisFrame->edge[whichEdge].edgePoints[0]);
  513.         MoveTo(myPoint->h, myPoint->v);
  514.         for (j = 1; j <= 5 ; ++j)
  515.         {
  516.            myPoint = &(thisFrame->edge[whichEdge].edgePoints[j]);
  517.            LineTo(myPoint->h, myPoint->v);        
  518.         }
  519.     }
  520.     PenSize(1,1);
  521.  }
  522. void  setForeColourNoShade(AGWindow *ag, grafData *thisPart)
  523. {
  524. #pragma unused(ag)
  525.     RGBForeColor (&(thisPart->colour)); 
  526. }
  527.  
  528. #ifndef __2DVectors__ 
  529. void  setForeColourWithShade(AGWindow *ag, grafData *thisGrafPtr)
  530. {
  531.     RGBColor shadedColour;
  532.     float range;
  533.     float neutralDepth;
  534.     float disp;
  535.     float factor;
  536.     
  537.     neutralDepth = ag->windowView.neutralShadeDepth * ag->windowView.magnification;
  538.     range = ag->windowView.shadeRange * ag->windowView.magnification;
  539.     disp = thisGrafPtr->graphc - neutralDepth;
  540.     shadedColour = thisGrafPtr->colour;
  541.     if (disp > 0)
  542.     {
  543.         factor = erf(disp/range);
  544.         shadedColour.red += (0xFFFF - thisGrafPtr->colour.red) * factor;
  545.         shadedColour.blue +=(0xFFFF - thisGrafPtr->colour.blue) * factor;
  546.         shadedColour.green +=(0xFFFF - thisGrafPtr->colour.green) * factor;
  547.     }
  548.     else if (disp < 0)
  549.     {
  550.         factor = erf(-disp/range);
  551.         shadedColour.red -= (0xFFFF - thisGrafPtr->colour.red) * factor;
  552.         shadedColour.blue -=(0xFFFF - thisGrafPtr->colour.blue) * factor;
  553.         shadedColour.green -=(0xFFFF - thisGrafPtr->colour.green) * factor;
  554.     }
  555.     RGBForeColor (&shadedColour); 
  556. }
  557. #endif
  558.  
  559. void  drawContents(AGWindow *ag)
  560. {
  561.     long i;
  562.     long count;
  563.     long * listPtr;
  564.     grafData *thisPart;
  565.     grafData *thisGrafPtr;
  566.     RGBColor blackRGB;
  567.     
  568.     agCheckOrderList(ag); 
  569.     thisGrafPtr =AGWindowLockGrafData( ag);
  570.     listPtr = lockList( &ag->orderList);
  571.     
  572. //    if (ag->transparentAtoms) PenMode (addOver);
  573. //    else 
  574.     PenMode (srcCopy);
  575.     PenSize (1,1);
  576.     blackRGB = getColour(myBlackColour);
  577.     count = AGWindowNumParts(ag);
  578.     if (ag->windowView.showBonds)
  579.     {
  580.         RGBForeColor (&blackRGB); 
  581.         PenPat(&qd.black); 
  582.         for (i= 0;  i<count ; ++i)
  583.         {
  584.             thisPart = &(thisGrafPtr[listPtr[i]]);
  585.             if (thisPart->inWindow)
  586.             {    
  587.             /*
  588.                 for (k = 0; k< thisPart->howManyBackbonds;++k)
  589.                 {
  590.                     MoveTo(thisPart->backbondStart[k].h,thisPart->backbondStart[k].v);
  591.                     LineTo(thisPart->backbondEnd[k].h,thisPart->backbondEnd[k].v);
  592.                 }
  593.                 */
  594.                 ag->setForeColour ( ag, thisPart); 
  595.                 
  596.                 
  597.                 if (!thisPart->selected)
  598.                 {
  599.                     PaintOval(&(thisPart->circle));
  600.                     RGBForeColor (&blackRGB); 
  601.                     FrameOval(&(thisPart->circle));
  602.                 }
  603.                 else
  604.                 {
  605.                     PenPat(&qd.gray); 
  606.                     PaintOval(&(thisPart->circle));
  607.                     PenPat(&qd.black); 
  608.                     PenSize (2,2);
  609.                     RGBForeColor (&blackRGB); 
  610.                     FrameOval(&(thisPart->circle));
  611.                     PenSize (1,1);
  612.                 }
  613.             /*    
  614.                 for (k = 0; k< thisPart->howManyForebonds;++k)
  615.                 {
  616.                     MoveTo(thisPart->forebondStart[k].h,thisPart->forebondStart[k].v);
  617.                     LineTo(thisPart->forebondEnd[k].h,thisPart->forebondEnd[k].v);
  618.                 }
  619.                 */
  620.             }
  621.             else
  622.             {
  623.             }
  624.         }
  625.     }
  626.     else // don't show bonds
  627.     {
  628.         PenPat(&qd.black); 
  629.         for (i= 0; i<count ; ++i)
  630.         {
  631.             thisPart = &(thisGrafPtr[listPtr[i]]);
  632.             if (thisPart->inWindow)
  633.             {    
  634.                 ag->setForeColour ( ag, thisPart); 
  635.                 if (!thisPart->selected)
  636.                 {
  637.                     
  638.                     PaintOval(&(thisPart->circle));
  639.                     RGBForeColor (&blackRGB); 
  640.                     FrameOval(&(thisPart->circle));
  641.                 }
  642.                 else
  643.                 {
  644.                     PenPat(&qd.gray); 
  645.                     PaintOval(&(thisPart->circle));
  646.                     PenPat(&qd.black); 
  647.                     PenSize (2,2);
  648.                     RGBForeColor (&blackRGB); 
  649.                     FrameOval(&(thisPart->circle));
  650.                     PenSize (1,1);
  651.                 }
  652.             }
  653.         }    
  654.     }
  655.     AGWindowReleaseGrafData(  ag);
  656.     releaseList( &ag->orderList);
  657. }
  658. void  drawHackContents(RGBColor *rgb, AGWindow *ag)
  659. {
  660.     long i;
  661.     long count;
  662.     long * listPtr;
  663.     grafData *thisPart;
  664.     grafData *thisGrafPtr;
  665.      
  666.     agCheckOrderList(ag); 
  667.     thisGrafPtr =AGWindowLockGrafData( ag);
  668.     listPtr = lockList( &ag->orderList);
  669.     
  670.      PenMode (srcCopy);
  671.     PenSize (1,1);
  672.      count = AGWindowNumParts(ag);
  673.     if (ag->windowView.showBonds)
  674.     {
  675.         RGBForeColor (rgb); 
  676.         PenPat(&qd.black); 
  677.         for (i= 0;  i<count ; ++i)
  678.         {
  679.             thisPart = &(thisGrafPtr[listPtr[i]]);
  680.             if (thisPart->inWindow)
  681.             {    
  682.             /*
  683.                 for (k = 0; k< thisPart->howManyBackbonds;++k)
  684.                 {
  685.                     MoveTo(thisPart->backbondStart[k].h,thisPart->backbondStart[k].v);
  686.                     LineTo(thisPart->backbondEnd[k].h,thisPart->backbondEnd[k].v);
  687.                 }
  688.                 */
  689.                 ag->setForeColour ( ag, thisPart); 
  690.                 
  691.                 
  692.                 if (!thisPart->selected)
  693.                 {
  694.                     PaintOval(&(thisPart->circle));
  695.                     RGBForeColor (rgb); 
  696.                     FrameOval(&(thisPart->circle));
  697.                 }
  698.                 else
  699.                 {
  700.                     PenPat(&qd.gray); 
  701.                     PaintOval(&(thisPart->circle));
  702.                     PenPat(&qd.black); 
  703.                     PenSize (2,2);
  704.                     RGBForeColor (rgb); 
  705.                     FrameOval(&(thisPart->circle));
  706.                     PenSize (1,1);
  707.                 }
  708.             /*    
  709.                 for (k = 0; k< thisPart->howManyForebonds;++k)
  710.                 {
  711.                     MoveTo(thisPart->forebondStart[k].h,thisPart->forebondStart[k].v);
  712.                     LineTo(thisPart->forebondEnd[k].h,thisPart->forebondEnd[k].v);
  713.                 }
  714.                 */
  715.             }
  716.             else
  717.             {
  718.             }
  719.         }
  720.     }
  721.     else // don't show bonds
  722.     {
  723.         RGBColor drawThisColor = *rgb;
  724.         drawThisColor.red = (65535 + rgb->red) / 2;
  725.         drawThisColor.green = (65535 + rgb->green) / 2;
  726.         drawThisColor.blue = (65535 + rgb->blue) / 2;
  727.         PenPat(&qd.black); 
  728.         for (i= 0; i<count ; ++i)
  729.         {
  730.             thisPart = &(thisGrafPtr[listPtr[i]]);
  731.             if (thisPart->inWindow)
  732.             {    
  733.                  if (!thisPart->selected)
  734.                 {
  735.                     RGBForeColor (&drawThisColor); 
  736.                     PaintOval(&thisPart->circle);
  737.                     RGBForeColor (rgb); 
  738.                     FrameOval(&(thisPart->circle));
  739.                 }
  740.             }
  741.         }    
  742.     }
  743.     AGWindowReleaseGrafData(  ag);
  744.     releaseList( &ag->orderList);
  745. }
  746.  
  747. void  findParticles(AGWindow *ag, particle *basePart, Point offset)
  748. {  // you must set up  the ag->atArray list before calling!
  749.      short err = noErr;
  750.      short j,  realSize, halfSize;
  751.     long howManyParts;
  752.      long *orderListPtr;
  753.     long * partListPtr;
  754.     grafData * theGrafPtr;
  755.     grafData *thisGrafPtr;
  756.     viewInfo *theView;
  757.      kSanVector graphicsCoords; // was graphA, graphB
  758.  //    kSanVector calcCoords;// was calcX, calcY
  759. //    float   depthFactor;    
  760. //    float sizeFactor; // cellVec, halfCellVec;
  761.     kSanMatrix  cMat;
  762.      // partSize;
  763.     OHScrollableView *sv = svData (ag->win->obj);
  764.     Rect visibleRect;
  765.  
  766.      visibleRect = sv->viewableBounds;
  767.      
  768.      agCheckPartList(ag);
  769.      agCheckOrderList(ag);
  770.     theGrafPtr = AGWindowLockGrafData(ag);
  771.     orderListPtr = lockList( &ag->orderList);
  772.     partListPtr = lockList( &ag->particleList);
  773.     howManyParts = AGWindowNumParts(ag) ;
  774.     
  775.     theView = &(ag->windowView);
  776.     
  777.     getCramerskSanOperatorsD(&(theView->viewMatrix), &cMat);
  778.     multkSanMatrixD(&cMat, theView->magnification, &cMat);
  779.     
  780.     for (j= 0; j<howManyParts ;++j)
  781.     {
  782.           //  copy the order of the particles in the grafData structure to the partList
  783.           partListPtr[j] = theGrafPtr[orderListPtr[j]].index ;
  784.     }
  785.     for (j= 0; j<howManyParts ;++j)
  786.     {
  787.         Rect *thisCircle;
  788.          long partArrIndex;
  789.         partArrIndex = partListPtr[j];
  790.  
  791. /*          calcX = basePart->basePosition[partArrIndex].a;
  792.         calcY = basePart->basePosition[partArrIndex].b;
  793. #ifndef __2DVectors__                
  794.         calcZ = basePart->basePosition[partArrIndex].c;
  795. #endif
  796. */
  797.         thisGrafPtr = &(theGrafPtr[j]);
  798.         thisGrafPtr->index = partArrIndex;
  799.         thisCircle = &(thisGrafPtr->circle);
  800.         
  801.          cramersCoordsOfkSanPoint ( &cMat, &basePart->basePosition[partArrIndex], &graphicsCoords);
  802.  
  803. /*        
  804. #ifndef __2DVectors__                
  805.         graphC = ((float) (calcX * (cMat.A.c) +
  806.                           calcY * (cMat.B.c) +
  807.                             calcZ  * (cMat.C.c) ) );
  808. #endif
  809.         graphA = ((float) (calcX * (cMat.A.a) +
  810.                           calcY * (cMat.B.a)
  811. #ifndef __2DVectors__                
  812.                           + calcZ  * (cMat.C.a) 
  813. #endif
  814.                                                 ) );
  815.                           
  816.         graphB = ((float) (calcX * (cMat.A.b) +
  817.                           calcY *  (cMat.B.b )
  818. #ifndef __2DVectors__                
  819.                           + calcZ  * (cMat.C.b ) 
  820. #endif
  821.                                                 ) );
  822.         
  823.                           
  824. #ifndef __2DVectors__                
  825.         depthFactor = viewLen / (viewLen - graphC);
  826. #endif
  827.         */
  828.         
  829.          thisGrafPtr->grapha =  graphicsCoords.a;
  830.          thisGrafPtr->graphb =  - graphicsCoords.b;                    
  831. #ifndef __2DVectors__                
  832.          thisGrafPtr->graphc =  graphicsCoords.c;        
  833. #endif
  834.         
  835. //        partSize = theView->particleSizeFactor;
  836.          realSize = ag->setMySize( ag, ag->atArray, theView, basePart, partArrIndex);
  837.  
  838.          halfSize = ((float) realSize) * 0.5;
  839.  
  840.         thisCircle->left = offset.h  + thisGrafPtr->grapha - halfSize;
  841.         thisCircle->right = thisCircle->left + realSize;
  842.         thisCircle->top = offset.v + thisGrafPtr->graphb - halfSize;
  843.         thisCircle->bottom = thisCircle->top  + realSize;
  844.  
  845.         if ((thisCircle->left > visibleRect.right) ||
  846.             (thisCircle->right < visibleRect.left) ||
  847.             (thisCircle->top > visibleRect.bottom) ||
  848.             (thisCircle->bottom < visibleRect.top)  )
  849.        {        
  850.             thisGrafPtr->inWindow = false;
  851.        }
  852.        else 
  853.        {
  854.             thisGrafPtr->inWindow = true;    
  855.             thisGrafPtr->selected = BPNthSelected(basePart, partArrIndex);
  856.        }
  857.      }
  858.     AGWindowReleaseGrafData(  ag);
  859.     releaseList( &ag->orderList);
  860.     releaseList( &ag->particleList);
  861. }
  862.  
  863.  
  864. void findBonds(AGWindow *ag, particle *basePart, Point offset)
  865. {  // you must set up  the ag->atArray list before calling!
  866. #ifdef __2DVectors__
  867. #pragma unused (ag)
  868. #endif
  869. #pragma unused(offset)
  870. #pragma unused(basePart)
  871. #ifndef __2DVectors__
  872.  //    long i, j,  numNNs;
  873.     long  numberOfParts;
  874.     grafData *theGrafPtr;
  875. //    grafData *thisGrafData;
  876.     long *  orderListPtr;   // need to create a label table to do cross-referencing
  877. //     particle * thisPart;
  878.     viewInfo *theView;
  879.     kSanMatrix cMat;
  880.        float viewLen;
  881. //    float depthFactor;
  882.  
  883.  agCheckOrderList(ag);
  884.     orderListPtr = lockList(&ag->orderList);
  885.     theGrafPtr = AGWindowLockGrafData( ag);
  886.     numberOfParts = AGWindowNumParts(ag);
  887.     
  888.     theView = &ag->windowView;
  889.      viewLen = theView->perspective * theView->magnification;
  890.     getCramerskSanOperatorsD(&theView->viewMatrix, &cMat);
  891.  
  892.     multkSanMatrixD(&cMat, theView->magnification, &cMat);
  893.     
  894.         /*
  895.     for (j= 0;  j<numberOfParts ;++j)  // j is index in theList
  896.     {
  897.         short howManyForebonds;
  898.         short howManyBackbonds;
  899.         Point *thisFStart;
  900.         Point *thisFEnd;
  901.         Point *thisBStart;
  902.         Point *thisBEnd;
  903.         NNInfo * nni;
  904.         floatVector centerVec, startVec, endVec, partVec, normBondVec;
  905.         floatVector tStartVec, tEndVec;
  906.         float bondLength, shortenBy;
  907.         float thisSize, thatSize;
  908.         
  909.         thisGrafData = &( theGrafPtr[j]);
  910.         if (!(thisGrafData->inWindow))
  911.         {  
  912.             thisGrafData->howManyForebonds = 0;
  913.             thisGrafData->howManyBackbonds = 0;
  914.         }
  915.         else    // its in the window --  lets make the bonds
  916.         {
  917.             thisPart =  &( basePart[thisGrafData->index]);  // this part has the particleData
  918.             thisBStart = thisGrafData->backbondStart;
  919.             thisBEnd = thisGrafData->backbondEnd;
  920.             thisFStart = thisGrafData->forebondStart;
  921.             thisFEnd = thisGrafData->forebondEnd;
  922.             partVec.a = thisPart->position.a;
  923.             partVec.b = thisPart->position.b; // double to float
  924.             partVec.c = thisPart->position.c;
  925.             thisSize = ag->setMyActualSize(agObj,ag, ag->atArray, theView, thisPart);
  926.             thisSize *= 0.5;
  927.             cramersCoordsOfkSanPointF(&cMat, &partVec, ¢erVec);
  928.             
  929.             //adjustForDepth(¢erVec, theView,  viewLen);  
  930.             // view len calculated above
  931.             depthFactor = viewLen / (viewLen - centerVec.c);        
  932.             centerVec.a =  (centerVec.a * depthFactor) ;
  933.              centerVec.b =  (centerVec.b * depthFactor) ;
  934.  
  935.  
  936.             howManyForebonds = 0;
  937.             howManyBackbonds = 0;
  938.             
  939.         // centerVec IS THE SCREEN COORDS OF THE  PARTICLE from which the bonds are coming
  940.             nni = lockNNInfo (&basePart[thisGrafData->index].neighbors);
  941.             i = 0;
  942.             numNNs = basePart[thisGrafData->index].neighbors.infoList.howManyItems;
  943.             while ((i<numNNs) && ((howManyForebonds < kMaxBonds) || (howManyBackbonds < kMaxBonds)) )
  944.             {
  945.                 // strategy first, check screening != zero
  946.                 if (nni[i].screen > 0)
  947.                 {
  948.                 // then find bond length.
  949.                     bondLength = nni[i].sep ;
  950.                         
  951.                     shortenBy = bondLength * 0.5;
  952.                     thatSize = ag->setMyActualSize(agObj,ag, ag->atArray, theView, &basePart[nni[i].nIndex]);
  953.                     thatSize *= 0.5;
  954.             // adjust end bond to either half the separation, or by radius of neighbor, whichever longer
  955.                     if (thatSize > shortenBy) shortenBy = thatSize;
  956.                     
  957.                     bondLength = bondLength - shortenBy;
  958.             // check that 
  959.                     if (bondLength > thisSize)
  960.                     {
  961.                         // adjust beginning of bond length by radius of part
  962.                         normBondVec.a = nni[i].vec.a * nni[i].invSep;
  963.                         normBondVec.b = nni[i].vec.b * nni[i].invSep;
  964.                         normBondVec.c = nni[i].vec.c * nni[i].invSep;
  965.                         // if length is positive, transform beginning and end points into points in graph space
  966.                         endVec.a = partVec.a + normBondVec.a * bondLength ;
  967.                         endVec.b = partVec.b + normBondVec.b * bondLength ;
  968.                         endVec.c = partVec.c + normBondVec.c * bondLength ;
  969.                         startVec.a = partVec.a + normBondVec.a * thisSize ;
  970.                         startVec.b = partVec.b + normBondVec.b * thisSize ;
  971.                         startVec.c = partVec.c + normBondVec.c * thisSize ;
  972.  
  973.                         cramersCoordsOfkSanPointF(&cMat, &endVec, &tEndVec);
  974.  
  975.                     //    adjustForDepth(&tEndVec,theView, viewLen);
  976.                     //  depth fac calculated above
  977.                          tEndVec.a = (tEndVec.a * depthFactor) ;
  978.                          tEndVec.b =  (tEndVec.b * depthFactor) ;
  979.  
  980.                         cramersCoordsOfkSanPointF(&cMat, &startVec, &tStartVec);
  981.  
  982.                     //    adjustForDepth(&tStartVec, theView, viewLen);
  983.                     //  depth fac calculated above
  984.                          tStartVec.a = (tStartVec.a * depthFactor) ;
  985.                          tStartVec.b = (tStartVec.b * depthFactor) ;
  986.  
  987.                         // check if its a forebond or back bond
  988.                         if (tEndVec.c > tStartVec.c)
  989.                         { // its a forebond
  990.                             if (howManyForebonds < kMaxBonds)
  991.                             {
  992.                                 thisFStart[howManyForebonds].h = tStartVec.a + offset.h;
  993.                                 thisFStart[howManyForebonds].v = offset.v - tStartVec.b;
  994.                                 thisFEnd[howManyForebonds].h = tEndVec.a + offset.h;
  995.                                 thisFEnd[howManyForebonds].v = offset.v - tEndVec.b;
  996.                                 ++howManyForebonds;
  997.                             } else {}
  998.                         }
  999.                         else // its a backbond
  1000.                         {
  1001.                             if (howManyBackbonds < kMaxBonds)
  1002.                             {
  1003.                                 thisBStart[howManyBackbonds].h = tStartVec.a + offset.h;
  1004.                                 thisBStart[howManyBackbonds].v = offset.v - tStartVec.b;
  1005.                                 thisBEnd[howManyBackbonds].h = tEndVec.a + offset.h;
  1006.                                 thisBEnd[howManyBackbonds].v = offset.v - tEndVec.b;
  1007.                                 ++howManyBackbonds;
  1008.                             }
  1009.                             else {}
  1010.                         }
  1011.                     } // end bond had positive length
  1012.                 } // end screening was not 0
  1013.                 ++i;  // effective loop over i ( number of neighbors )
  1014.             }  // end while (loop over neighbors 
  1015.             thisGrafData->howManyForebonds = howManyForebonds;
  1016.             thisGrafData->howManyBackbonds = howManyBackbonds;
  1017.             releaseNNInfo(&basePart[thisGrafData->index].neighbors);  
  1018.         }  // end of this part is in window
  1019.     }  // end of loop over number of parts in window
  1020.         */
  1021.     releaseList(&ag->orderList);
  1022.     AGWindowReleaseGrafData(  ag);
  1023.     #endif
  1024. }
  1025. //void adjustForDepth(floatVecPtr centerVec, viewInfo *theView, float viewLen)
  1026. //{
  1027. //    float depthFactor;
  1028.  
  1029. //    depthFactor = viewLen / (viewLen - centerVec->c);        
  1030. //    centerVec->a = theView->cellCenter.h + (centerVec->a * depthFactor) ;
  1031. //    centerVec->b = theView->cellCenter.v - (centerVec->b * depthFactor) ;
  1032. //}
  1033.  
  1034. void  screenCoordsOfXYZPoint ( viewInfo *theView, kSanVector * thePos, kSanVector * theCoords);
  1035. void  screenCoordsOfXYZPoint ( viewInfo *theView, kSanVector * thePos, kSanVector * theCoords)
  1036. {
  1037.      float depthFactor;    
  1038.     kSanMatrix  cMat;
  1039.     float mag, viewLen;
  1040.     
  1041. #ifdef __2DVectors__
  1042. #pragma unused (depthFactor)
  1043. #endif
  1044.  
  1045.     mag = theView->magnification;
  1046.     viewLen = theView->perspective * mag;
  1047.  
  1048.     getCramerskSanOperatorsD(&(theView->viewMatrix), &cMat);
  1049.     multkSanMatrixD(&cMat, mag, &cMat);
  1050.     cramersCoordsOfkSanPoint(&cMat, thePos, theCoords);
  1051.                           
  1052. #ifndef __2DVectors__
  1053.     depthFactor = viewLen / (viewLen - theCoords->c);
  1054.         
  1055.      theCoords->a = theView->lastOffsetUsed.h + (theCoords->a  * depthFactor) ;
  1056.     theCoords->b = theView->lastOffsetUsed.v - (theCoords->b * depthFactor) ;
  1057. #else
  1058.      theCoords->a = theView->lastOffsetUsed.h + (theCoords->a  ) ;
  1059.     theCoords->b = theView->lastOffsetUsed.v - (theCoords->b  ) ;
  1060. #endif
  1061. }
  1062.  
  1063.  
  1064. float setNoActualSize(AGWindow *ag, kSanAtomtype **atArray, viewInfo *theView,  particle *basePart, long partArrIndex)
  1065. {
  1066. #pragma unused(ag)
  1067. #pragma unused(basePart)
  1068. #pragma unused(partArrIndex)
  1069.  #pragma unused(atArray)
  1070.     return (5.0 / theView->magnification);
  1071. }
  1072.  
  1073. float setMyActualSizeByAtomType(AGWindow *ag, kSanAtomtype **atArray, viewInfo *theView, particle *basePart, long partArrIndex)
  1074. {
  1075. #pragma unused(ag)
  1076. #pragma unused(theView)
  1077.      return ( atArray[BPNthType(basePart, partArrIndex)]->specs.drawingRadius  );
  1078. }
  1079.  
  1080. float setMyActualSizeByWindow(AGWindow *ag, kSanAtomtype **atArray,viewInfo *theView,  particle *basePart, long partArrIndex)
  1081. {
  1082.  #pragma unused(ag)
  1083.     return ( atArray[BPNthType(basePart, partArrIndex)]->specs.drawingRadius  * theView->particleSizeFactor );
  1084. }
  1085.  
  1086. float setNoSize(AGWindow *ag, kSanAtomtype **atArray,viewInfo *theView, particle *basePart, long partArrIndex)
  1087. {
  1088. #pragma unused(ag)
  1089.  #pragma unused(theView)
  1090. #pragma unused(basePart)
  1091. #pragma unused(partArrIndex)
  1092. #pragma unused(atArray)
  1093.     return (5.0);
  1094. }
  1095.  
  1096. float setMySizeByAtomType(AGWindow *ag, kSanAtomtype **atArray,viewInfo *theView, particle *basePart, long partArrIndex)
  1097. {
  1098. #pragma unused(ag)
  1099.      return (atArray[BPNthType(basePart, partArrIndex)]->specs.drawingRadius * theView->magnification);
  1100. }
  1101.  
  1102. float setMySizeByWindow(AGWindow *ag, kSanAtomtype **atArray,viewInfo *theView,  particle *basePart, long partArrIndex)
  1103. {
  1104.  #pragma unused(ag)
  1105.     return (atArray[BPNthType(basePart, partArrIndex)]->specs.drawingRadius * theView->magnification * theView->particleSizeFactor);
  1106. }
  1107.  
  1108.